﻿// (c) Copyright Max Kazarin.
// This source is subject to the Microsoft Permissive License.
// See http://www.microsoft.com/resources/sharedsource/licensingbasics/sharedsourcelicenses.mspx.
// All other rights reserved.

/// <reference path='../Common/jquery-1.3.2.js' />
// <reference path='../Common/MicrosoftAjax.debug.js' />

Type.registerNamespace('jQueryNET.Controls');

jQueryNET.Controls.FieldBase = function jQueryNET$Controls$FieldBase(element) {
	/// <summary>
	/// This class implement FieldBase control
	/// </summary>
	/// <param name='element' type='Sys.UI.DomElement' domElement='true'>
	/// DOM element the control is associated with
	/// </param>

	jQueryNET.Controls.FieldBase.initializeBase(this, [element]);

	this._rootElement = $(this.get_element());
}

function jQueryNET$Controls$FieldBase$initialize() {
	/// <summary>
	/// Initialize the control
	/// </summary>

	jQueryNET.Controls.FieldBase.callBaseMethod(this, 'initialize');
}

function jQueryNET$Controls$FieldBase$dispose() {
	/// <summary>
	/// Dispose the control
	/// </summary>
	jQueryNET.Controls.FieldBase.callBaseMethod(this, 'dispose');
}

function jQueryNET$Controls$FieldBase$get_allowBlank() {
	/// <value type='String'>
	/// Allow blank status
	/// </value>

	if (arguments.length !== 0) throw Error.parameterCount();
	return this._allowBlank;
}

function jQueryNET$Controls$FieldBase$set_allowBlank(value) {
	var e = Function._validateParams(arguments, [{ name: "value", type: Boolean, mayBeNull: false}]);
	if (e) throw e;

	this._allowBlank = value;
}

function jQueryNET$Controls$FieldBase$validate() {
	var result = true;
	Array.forEach(
		this._validators,
		function(element, index, array){
			result &= element(this);
		},
		this);
	
	if (!result)
		this.mark();
		
	return result;
}

function jQueryNET$Controls$FieldBase$mark() {
	throw Error.notImplemented();
}

function jQueryNET$Controls$FieldBase$add_validator(handler) {
	/// <summary>
	/// Add a validator function
	/// </summary>
	/// <param name='handler' type='Function' mayBeNull='false'>
	/// Event handler
	/// </param>
	/// <returns />
	var e = Function._validateParams(arguments, [{name: "handler", type: Function, mayBeNull: false}]);
	if (e) throw e;
	
	if (!Array.contains(array, handler))
		array.push(handler)
}

function jQueryNET$Controls$FieldBase$remove_validator(handler) {
	/// <summary>
	/// Remove a validator function
	/// </summary>
	/// <param name='handler' type='Function' mayBeNull='false'>
	/// Event handler
	/// </param>
	/// <returns />
	var e = Function._validateParams(arguments, [{name: "handler", type: Function, mayBeNull: false}]);
	if (e) throw e;

	Array.remove(array, handler);
}

jQueryNET.Controls.FieldBase.prototype = {
	_rootElement: null,
	_validators: new Array(),
	
	_allowBlank: true,
	
	initialize: jQueryNET$Controls$FieldBase$initialize,
	dispose: jQueryNET$Controls$FieldBase$dispose,

	get_allowBlank: jQueryNET$Controls$FieldBase$get_allowBlank,
	set_allowBlank: jQueryNET$Controls$FieldBase$set_allowBlank,
	
	validate: jQueryNET$Controls$FieldBase$validate,
	mark: jQueryNET$Controls$FieldBase$mark,

	add_validate: jQueryNET$Controls$FieldBase$add_validator,
	remove_validate: jQueryNET$Controls$FieldBase$remove_validator
}

jQueryNET.Controls.FieldBase.registerClass('jQueryNET.Controls.FieldBase', AjaxControlToolkit.ControlBase);
